#if 0
//P2024 start from 1
#include <iostream>
#define MAX_N 50000
using namespace std;

int Find(int x)
{
    int r = x;
    while (par[r]!=r) r=par[r];
    while (par[x] != r)
    {
        int tmp = par[x];
        par[x] = r;
        x = tmp;
    }
    return r;
}

bool Connected(int x, int y)
{
    return (Find(x) == Find(y));
}

void Union(int x, int y)
{
    int rx = Find(x), ry = Find(y);
    if (sz[rx] > sz[ry])
    {
        par[ry] = rx;
        sz[rx] += sz[ry];
    }
    else
    {
        par[rx] = ry;
        sz[ry] += sz[rx];
    }
}

int main()
{
    int num_n, num_cmd, cnt = 0;
    cin >> num_n >> num_cmd;
    int par[num_n+1];
    for (int i=1; i<=num_n; ++i) par[i] = i;

    for (int i=1; i<=num_cmd; ++i)
    {
        int op, x, y;
        cin >> op >> x >> y;
        if (x > num_n || y >num_n) {++cnt;continue;}
        if (op = 1)
        {
            if (x==y) continue;
            if (par[x]=x && par[y]=y) {Union(x, y);continue;}
            if (!Connected(x, y)) {++cnt; continue;}
            else continue;
        }
        if (op = 2)
        {
            if (x==y){++cnt; continue;}



        }
    }
    return 0;
}
#endif
#if 0
//P1197 start from 0
#include <iostream>
#define MAX_N = 400000
#define MAX_R = 200000
using namespace std;

int par[MAX_N], sz[MAX_N];
int main()
{
    int num_n, num_r;
    cin >> num_n >> num_r;
    for (int i=0; i<num_r; ++i)
    {
        int x, y;
        cin >> x >> y;
        Union(x, y);
    }

    return 0;
}
#endif

//LUO_P1196
#include <iostream>
#include <algorithm>
#include <list>
#include <cmath>
#define MAX_N 30000+1
#define MAX_R 100000+1
using namespace std;

int par[MAX_N], sz[MAX_N], id[MAX_N];
list<int> son[MAX_N];

int Find(int x)
{
    int r = x;
    while (par[r]!=r)
        r=par[r];
    while (par[x] != r)
    {
        int tmp = par[x];
        par[x] = r;
        son[r].push_back(x);
        x = tmp;
    }
    return r;
}

bool Connected(int x, int y)
{
    return (Find(x) == Find(y));
}

void MakeID(int siz, int x)
{
    id[x] += siz;
    list<int>::iterator it = son[x].begin();
    list<int>::iterator it_end = son[x].end();
    while (it != it_end)
    {
        MakeID(siz, *it);
        ++it;
    }

    //list<int>::iterator it_end = son[x].end();
    //for (const auto& ite : son[x]) cout << ite << " "; cout << endl;
    /*while (it != son[x].end())
    {
        id[*it] += siz;

        ++it;
    }*/

    /*for (int i=1; i<=MAX_N; ++i)
    {
        if (Find(i) == x) id[i] += siz;
    }*/
}

void Union(int x, int y)
{
    int rx = Find(x), ry = Find(y);//rx:root of x
    MakeID(sz[ry], rx);
    if (sz[rx] > sz[ry])
    {
        par[ry] = rx;
        son[rx].push_back(ry);
        sz[rx] += sz[ry];

    }
    else
    {
        par[rx] = ry;
        sz[ry] += sz[rx];
        son[ry].push_back(rx);

    }




}

int main()
{
    //Input, start from 1
    int num_t;
    cin >> num_t;
    for (int i=1; i<=MAX_N; ++i)
    {
        par[i] = i;
    }
    fill(sz, sz+MAX_N, 1);
    fill(id, id+MAX_N, 1);

    char op;
    int x, y;
    for (int i=1; i<=num_t; ++i)
    {
        cin >> op >> x >> y;
        if (op == 'M')
        {
            Union(x, y);
        }
        if (op == 'C')
        {
            if (!Connected(x, y))
                cout << -1 << endl;
            else
            {
                int mid = id[x] - id[y];
                cout << abs(mid)-1 << endl;
            }
        }
    }
    return 0;
}

/*LUO_P1111 Disjoint Set Union
#include <iostream>
#include <algorithm>
#define MAX_N 1000+1
#define MAX_R 100000+1
using namespace std;

typedef struct
{
    int x, y, time;
}Road;
int par[MAX_N], sz[MAX_N];
int cnt;

bool cmp(Road x, Road y)
{
    return (x.time < y.time);
}


int Find(int x)
{
    int r = x;
    while (par[r]!=r) r=par[r];
    while (par[x] != x)
    {
        int tmp = par[x];
        par[x] = r;
        x = tmp;
    }
    return r;
}

bool Connected(int x, int y)
{
    return (Find(x) == Find(y));
}

void Union(int x, int y)
{
    int rx = Find(x), ry = Find(y);
    if (sz[rx] > sz[ry])
    {
        par[ry] = rx;
        sz[rx] += sz[ry];
    }
    else
    {
        par[rx] = ry;
        sz[ry] += sz[rx];
    }
    --cnt;
}

int main()
{
    //Input, start from 1
    int num_n, num_t;
    cin >> num_n >> num_t;
    for (int i=1; i<=num_n; ++i) par[i] = i;
    fill(sz, sz+num_n+1, 1);
    cnt = num_n;

    Road r[num_t+1];
    for (int i=1; i<=num_t; ++i)
    {
        cin >> r[i].x >> r[i].y >> r[i].time;
    }

    int total = 0;//total time
    sort(r+1, r+num_t+1, cmp);
    for (int i=1; i<=num_t; ++i)
    {
        if (Connected(r[i].x, r[i].y)) continue;
        if (total < r[i].time) total = r[i].time;
        Union(r[i].x, r[i].y);
    }
    if (cnt == 1) cout << total;
    else cout << -1;
    return 0;
}
*/
